<?php

/**
 * This is the model class for table "tbl_teaching".
 *
 * The followings are the available columns in table 'tbl_teaching':
 * @property integer $teaching_id
 * @property string $title
 * @property string $description
 * @property integer $user_id
 * @property string $create_date
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property TblUser $user
 * @property TblResources[] $tblResources
 */
class Teaching extends CActiveRecord
{
	const STATUS_ACTIVE = 1;
    const STATUS_DEACTIVE = 0;

    const TYPE_INSTRUMENT = 2;
    const TYPE_TEACH = 1;
    const TYPE_PLATFORM = 3;

    /**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Teaching the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_teaching';
	}

	/**
	 * @return array validation rules for model attributes.
     */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, description, user_id, status, active', 'required'),
			array('user_id, status, active', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('teaching_id, title, description, user_id, create_date, status, active', 'safe', 'on'=>'search'),
        );
    }
    public function getActive()
    {
        return $this->active == Self::STATUS_ACTIVE ? "显示" : "不显示";
    }
    public function getActiveOptions()
    {
        return array(Teaching::STATUS_ACTIVE=>'显示', Teaching::STATUS_DEACTIVE=>'不显示');
    }
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'resources' => array(self::MANY_MANY, 'Resources', 'tbl_teaching_resources(teaching_id, resources_id)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'teaching_id' => '教学资源',
			'title' => '资源名称',
			'description' => '资源描述',
			'user_id' => '资源创建人',
			'create_date' => '创建日期',
			'status' => '资源类型',
			'active' => '是否显示',
		);
	}
    /**
     * 返回资源类型
     *
     */
    public function getType()
    {
        return $this->status == Self::TYPE_TEACH ? "学习资源" : "平台资源";
    }
/**
     * 返回资源类型
     *
     */
    public function getOptions()
    {
        return array(Teaching::TYPE_TEACH=>'学习资源', Teaching::TYPE_PLATFORM=>'平台资源');
    }
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('teaching_id',$this->teaching_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('active',$this->active);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}
